<?php
/**
* @author: trungdt
* Change logs
* Aug 26, 2009 - trungdt - first create
* Jan 23, 2011 - trungdt - implement beter, look like  gitorious.org
* Oct 14, 2011 - trungdt - Add specical parameter: input_param: contain array of params
*/
/* {paging current_page=xx pages=yy action=zzz module=tt ...}
  current_page: your current page
  pages: how many pages
  lang_page: the word "Page"
  action: link action
  module: link module
  input_param: array (
    key => value
  ) : optional, auto map input_param into $params. Its the way we build link easier
  ...
  you can set as many parameter as you want. 
  All parameter will be used to build up the href link
*/
function smarty_function_paging($params, &$smarty)
{
    $max_side_length=6; // $max_length=2*$max_side_length 
    if (!in_array('current_page', array_keys($params))) {
        $smarty->trigger_error("paging: missing 'current_page' parameter");
        return;
    }
    if (!in_array('pages', array_keys($params))) {
        $smarty->trigger_error("paging: missing 'pages' parameter");
        return;
    }
    // build up the HTML
    // cache language
    if (isset($params['page_length']))
      {
         $max_side_length=$params['page_length'];
         unset($params['page_length']);
      }
    if (isset($params['lang_page']))
    {
      $lang_page=$params['lang_page'];
      $lang_first=$params['lang_first'];
      $lang_last=$params['lang_last'];
      $lang_prev=$params['lang_prev'];
      $lang_next=$params['lang_next'];
      unset($params['lang_prev'], $params['lang_next'], $params['lang_last'], $params['lang_first'], $params['lang_page']);
    } else {
      // here, we load default language 
      // 9/5/2009: immanuel192 add default language
      $i18n=exponent_lang_loadFile("plugins/function_paging.php");
      $lang_page=$i18n['lang_page'];
      $lang_first=$i18n['lang_first'];
      $lang_last=$i18n['lang_last'];
      $lang_prev=$i18n['lang_prev'];
      $lang_next=$i18n['lang_next'];
    }  
    
    // Oct 14 , 2011 - trungdt - Input param map
    if (isset($params['input_param']) && is_array($params['input_param'])){
        foreach ($params['input_param'] as $k=>$v)
            $params[$k]=$v;
        unset($params['input_param']);
    }
    // Oct 14 , 2011 - trungdt - End Input param map
    
    $html="<div class='abpagination'>"; 
    $current_page=intval($params['current_page']); $pages=intval($params['pages']);
    unset($params['current_page'],$params['pages']);
    //
    if ( $pages==1) return; // nothing to do here
    if ($current_page>$pages)
    {
        $current_page=$pages;
    }
    //
    $start_offset=1; $end_offset=$max_side_length;
    
    if ($pages > $max_side_length)
    {
      $start_offset=$current_page - floor($max_side_length/2);
      if ($start_offset<1)
      {
        $start_offset=1;
      }
      $end_offset=$start_offset + $max_side_length ;
      if ($end_offset>$pages)
        $end_offset=$pages;  
    } else { $end_offset=$pages; }
    if ($end_offset==0) return;
    // the word Page
    // Jan 23, 2011 - trungdt
    // we dont need this word now :)
    //$html.=$lang_page."&nbsp;";
    // first
    if ($current_page > 1){
      //// Jan 23, 2011 - trungdt - force to show first page, no need this code
      //$params['page']=1;
      //$link = exponent_core_makeLink($params);
      //$html.="<span class='first'><a href='{$link}'>{$lang_first}</a></span>";
      // previous
      $params['page']=$current_page-1;
      $link = exponent_core_makeLink($params);
      $html.="<a class='prev_page' href='{$link}' title='{$lang_prev}' alt='{$lang_prev}'>{$lang_prev}</a>";
    }
    else
    {
        // show disable previous class
        $html.="<span class='disabled prev_page'>{$lang_prev}</span>";
    }
    // Jan 23, 2011 - trungdt - if start_offset > 1, 
    if ($start_offset > 2)
    {
       $params['page']=1;
       $link = exponent_core_makeLink($params);
       $html.="<a href='{$link}'>1</a>"; 
       // for ...
       $html.="<span class='gap'>…</span>  ";
    }
    // page list here
    for ($i=$start_offset;$i<=$end_offset;$i++)
    {
      if ($current_page == $i)
      {
          $html.="<span class='current'>{$i}</span>";
      }
      else
      {
        $params['page']=$i;
        $link = exponent_core_makeLink($params);        
        $html.="    <a href='{$link}' >{$i}</a>"   ;
      }
    }
  
    // Jan 23, 2011 - trungdt - if end offset not reach to $pages
    if ($end_offset < $pages)
    {
        // for ...
        $html.="<span class='gap'>…</span>  "; 
        
        $params['page']=$pages;
        $link = exponent_core_makeLink($params);
        $html.="<a href='{$link}'>{$pages}</a>"; 
    }
  
    if ($current_page < $pages){
      // next
      $params['page']=$current_page+1;
      $link = exponent_core_makeLink($params);
      $html.="<a href='{$link}' class='next_page' title='{$lang_next}' alt='{$lang_next}'>{$lang_next}</a></span>";
      // last
      // Jan 23,m 2011 - force to show last page, no need this
      //$params['page']=$pages;
      //$link = exponent_core_makeLink($params);
      //$html.="<span class='last'><a href='{$link}'>{$lang_last}</a></span>";      
    }
    else
    {
        // disable next button
        // show disable previous class
        $html.="<span class='disabled next_page'>{$lang_next}</span>";
    }
    //
    $html.="</div>";
    return $html;
}
?>